package com.catt.orderdinner.controller;

import com.catt.orderdinner.form.ApiReturnObj;
import com.catt.orderdinner.form.OrderForm;
import com.catt.orderdinner.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/order")
@ResponseBody
public class OrderController {

    @Autowired
    private OrderService orderService;
    @RequestMapping("commit")
    public ApiReturnObj commitOrder(String userId, String selectFoods, String amount){
        ApiReturnObj apiReturnObj = new ApiReturnObj();
        apiReturnObj.setCode("200");
        apiReturnObj.setMessage("操作成功");
        try {
            String[] foodIds = selectFoods.split("-");
            orderService.commitOrder(userId,foodIds,amount);
        } catch (Exception e){
            apiReturnObj.setCode("400");
            apiReturnObj.setMessage("服务器错误");
        }
        return apiReturnObj;
    }

    @RequestMapping("list")
    public List<OrderForm> listOrder(String userId){
        List<OrderForm> list = orderService.list(userId);
        return list;
    }
}
